package com.wontube.smr.remote;

import java.util.List;

import com.wontube.client.extension.utils.StringUtils;
import com.wontube.client.modle.vo.PagenateInfo;
import com.wontube.core.annotation.CacheSupport;
import com.wontube.core.annotation.NoLoginSupport;
import com.wontube.core.pub.servlet.handlers.BasicHandler;
import com.wontube.smr.bo.sites.MVSelectBO;
import com.wontube.smr.bo.sites.MovieSelectBO;
import com.wontube.smr.bo.sites.VideoSelectBO;
import com.wontube.smr.client.IDeviceHandler;
import com.wontube.smr.client.IShortFilmHandler;
import com.wontube.smr.client.IVideoHandler;
import com.wontube.smr.client.model.po.MVChart;
import com.wontube.smr.client.model.po.WebsiteLink;
import com.wontube.smr.client.model.vo.DeviceHomeItems;
import com.wontube.smr.client.model.vo.DeviceSearchItems;
import com.wontube.smr.client.model.vo.EpisodeItems;
import com.wontube.smr.client.model.vo.MovieItems;
import com.wontube.smr.client.model.vo.SearchCountPreview;
import com.wontube.smr.client.model.vo.TVShowItems;
import com.wontube.smr.client.model.vo.VVideoItems;
import com.wontube.smr.dao.bo.AnimeSelectDao;
import com.wontube.smr.dao.bo.TVSelectDao;
import com.wontube.smr.dao.bo.WebsiteLinkSelectDao;


/**
 * @author yuechao
 *
 */
@NoLoginSupport
public final class VideoHandler extends BasicHandler implements IVideoHandler {
	@Override
	@CacheSupport
	public VVideoItems search(Double dbFactor, String keyword, PagenateInfo pageInfo) {
		if (dbFactor == null) {
			return null;
		}
		
		try {
			return VideoSelectBO.searchAllVideosForMobile(dbFactor, keyword, pageInfo);
		} catch (Exception e) {
			throw new RuntimeException(e);
		} 
	}
	
	@Override
	@CacheSupport
	public SearchCountPreview previewItemsCount(Double dbFactor, String keyword) {
		if (dbFactor == null) {
			return null;
		}
		
		try {
			return VideoSelectBO.previewItemsCount(keyword);
		} catch (Exception e) {
			throw new RuntimeException(e);
		} 
	}
	
	@Override
	@CacheSupport
	public DeviceSearchItems deviceSearch(String keyword) {
		DeviceSearchItems items = new DeviceSearchItems();
		PagenateInfo pageInfo = new PagenateInfo(1, 2);
		
		//hulu tv
		TVHandler tvHandler = new TVHandler();
		TVShowItems tvShowItems = tvHandler.searchTVShows(keyword, pageInfo);
		items.setTvShowItems(tvShowItems);
		EpisodeItems episodeItems = tvHandler.searchEpisodes(keyword, pageInfo);
		items.setEpisodeItems(episodeItems);
		
		//动漫
		TVShowItems animeItems = TVSelectDao.searchCrunchyrollAnimes(keyword, pageInfo);
		items.setAnimeItems(animeItems);
		EpisodeItems animeEpisodeItems = TVSelectDao.searchCrunchyrollAnimeEpisodes(keyword, pageInfo);
		items.setAnimeEpisodeItems(animeEpisodeItems);
		
		//hulu movie
		MovieItems movieItems = new MovieHandler().search(keyword, 0.8, pageInfo);
		items.setMovieItems(movieItems);
		
		//vevo mv
		MovieItems mvItems = new MVHandler().searchMusic(keyword, 0.8, pageInfo);
		items.setMvItems(mvItems);
		
		//youtube short film
		MovieItems shortFilmItems = new ShortFilmHandler().searchVideos(keyword, pageInfo);
		items.setShortFilmItems(shortFilmItems);				
				
		return items;
	}
	
	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public DeviceHomeItems listDeviceHomeItems(String sysId, String countryCode) {
		DeviceHomeItems items = new DeviceHomeItems();
		if (StringUtils.isNullOrEmpty(sysId)) {
			return items;
		}
		
		final PagenateInfo pageInfo = new PagenateInfo(1, 10);
		
		TVShowItems tvShowItems = TVSelectDao.selectHuluPopularShows(pageInfo);
		items.setTvShowItems(tvShowItems);
		
		//动漫
		TVShowItems animeItems = AnimeSelectDao.selectPopularAnimes(pageInfo);
		items.setAnimeItems(animeItems);
		
		EpisodeItems episodeItems = TVSelectDao.selectHuluPopularEpisodes(pageInfo);
		items.setEpisodeItems(episodeItems);

		MovieItems movieItems = MovieSelectBO.selectHuluPopular(0.8, pageInfo);		
		items.setMovieItems(movieItems);
		
		MovieItems mvItems = MVSelectBO.selectMusics(MVChart.UPDATE_PERIOD_WEEKLY, null, 0.8, pageInfo);		
		items.setMvItems(mvItems);
		
		MovieItems shortFilmItems = new ShortFilmHandler()
			.listPopular(IShortFilmHandler.TIME_THIS_WEEK, pageInfo);
		items.setShortFilmItems(shortFilmItems);
		List<WebsiteLink> links = WebsiteLinkSelectDao.listDeviceHome(sysId, countryCode);
		items.setLinks(links);
		
		return items;
	}	
	
	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public DeviceHomeItems listDeviceHomeItems(String sysId) {
		return this.listDeviceHomeItems(sysId, "US");
	}

	@Override
	@CacheSupport(timeToLiveSeconds = 6 * 3600)
	public DeviceHomeItems listDeviceHomeItems() {
		return this.listDeviceHomeItems(IDeviceHandler.sysIdAndroid);
	}

	@Override
	@CacheSupport
	public String getThumbnail(String url) {
		try {
			String thumbnail = VideoSelectBO.getThumbnail(url);
			return thumbnail;
		} catch (Exception e) {
			throw new RuntimeException(e);
		} 
	}
}
